import React, {useReducer} from 'react'

export default function UseReducer() {
  const initState = {
    age: 18,
    name: 'Tom'
  }
  const reducer = (state = initState, action)=>{
    switch (action.type) {
      case 'add':
        return {
          ...state,
          age: state.age + 1
        }
      default:
        return state
    }
  }
  const [state, dispatch] = useReducer(reducer, initState);
  const changeAge = ()=>{
    dispatch({type: 'add'})
  }
  return (
    <div>
      <p>名字：{state.name}</p>
      <p>年龄：{state.age}</p>
      <button onClick={()=>changeAge()}>年龄+1</button>
    </div>
  )
}
